﻿using System.Globalization;
using BaseData;
using BaseData.Exts;
using BaseData.HelperEzui;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// NewsAdminNodeListTree 的摘要说明
    /// </summary>
    public class NewsAdminNodeListTree : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            context.Response.Write(this.TreeNode(context, context.Request.Form["id"].ToInt(-1)).ObjectsToJson());
            context.Response.ContentType = "text/plain";
        }
        /// <summary>
        /// 整个树节点
        /// </summary>
        /// <param name="context"></param>
        /// <param name="reqId"></param>
        /// <returns></returns>
        private List<EzuiTreeView<Web_Base_News_Node>> TreeNode(HttpContext context, int reqId = 0)
        {
            using (var ctx = new ContextExt())
            {
                var temps = this.GetNodes(ctx, reqId);
                var items = temps.ToList();
                items.ForEach(f =>
                {
                    var nodes = this.GetNodes(ctx, f.id.ToInt(0));
                    if (nodes.Any())
                    {
                        var node = nodes.ToList();
                        node.ForEach(e =>
                        {
                            var lowers = this.GetNodes(ctx, e.id.ToInt(0));
                            if (lowers.Any()) e.state = new TreeNodes().GetState(lowers, e.id.ToInt(0)); // 最内层，不需要 e.children;
                        });
                        f.children = node.ToList();
                    }
                    f.attributes = new Attributes { key = f.id.ToInt(0), value = f.id.ToInt(0), };
                    f.state = new TreeNodes().GetState(nodes, reqId);
                });
                return reqId != 0 ? items : new TreeNodes().GetRoot(items);
            }
        }
        /// <summary>
        /// 当前ID的下级节点
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private IQueryable<EzuiTreeView<Web_Base_News_Node>> GetNodes(ContextExt ctx, int id)
        {
            return from q in ctx.Web_Base_News_Node
                   where q.ParentId == id
                   select new EzuiTreeView<Web_Base_News_Node>
                   {
                       id = q.PkId,
                       parentid = (q.ParentId ?? 0),
                       text = q.Title,
                       attributes = new Attributes { key = q.PkId, value = q.PkId, },
                   };
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}